package cn.usian.service.impl;

import cn.usian.jyzx.model.dto.h5.ProductSkuDto;
import cn.usian.jyzx.model.entity.product.Category;
import cn.usian.jyzx.model.entity.product.Product;
import cn.usian.jyzx.model.commot.RedisCommot;
import cn.usian.jyzx.model.entity.product.ProductSku;
import cn.usian.jyzx.model.vo.common.Result;
import cn.usian.jyzx.model.vo.common.ResultCodeEnum;
import cn.usian.jyzx.model.vo.h5.IndexVo;
import cn.usian.jyzx.model.vo.h5.ProductItemVo;
import cn.usian.mapper.CategoryMapper;
import cn.usian.mapper.ProductMapper;
import cn.usian.mapper.ProductskuMapper;
import cn.usian.service.ProductService;
import com.alibaba.fastjson2.JSON;
import com.alibaba.fastjson2.JSONArray;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import java.util.Arrays;
import java.util.HashMap;
import java.util.List;

@Service
public class ProductServiceimpl implements ProductService {
    @Autowired
    ProductMapper productMapper;
    @Autowired
    CategoryMapper categoryMapper;
    @Autowired
    RedisTemplate<String,String> redisTemplate;
    @Autowired
    ProductskuMapper productskuMapper;

    @Override
    public Result findAll() {
        List<Category> categoriedList = categoryList();
        List<ProductSku> productSkuList = productMapper.findS();
        IndexVo indexVo = new IndexVo();
        indexVo.setCategoryList(categoriedList);
        indexVo.setProductSkuList(productSkuList);
        return Result.build(indexVo, ResultCodeEnum.SUCCESS);
    }

    @Override
    public Result<PageInfo<ProductSkuDto>> productSkuDtoPageInfo(Integer page, Integer limit, ProductSkuDto productSkuDto) {
        PageHelper.startPage(page,limit);
        List<ProductSkuDto> productSkuDtoList=productMapper.productSkuDtoPageInfo(productSkuDto);
        PageInfo<ProductSkuDto> info = new PageInfo<>(productSkuDtoList);
        return Result.build(info,ResultCodeEnum.SUCCESS);
    }

    @Override
    public Result<ProductItemVo> item(Long id) {
        //商品sku信息
        ProductSku productSku = productskuMapper.seleSkuId(id);
        //商品信息
        Product product = productMapper.findById(productSku.getProductId());
        String sliderUrls = product.getSliderUrls();
        String[] split = sliderUrls.split(",");
        //商品轮播图
        List<String> sliderUrl = Arrays.asList(split);

        String detailsImg = productMapper.findDetailsImageUrl(product.getId());
        //商品详情图片列表
        List<String> detailsImageUrl = Arrays.asList(detailsImg.split(","));

        String specValue = product.getSpecValue();
        //商品规格信息
        com.alibaba.fastjson.JSONArray array = com.alibaba.fastjson.JSON.parseArray(specValue);

        //商品规格对应商品skuId信息
        HashMap<String, Object> stringObjectHashMap = new HashMap<>();
        List<ProductSku> list = productMapper.findSkuSpecVlueMap(product.getId());
        for (ProductSku sku : list) {
            stringObjectHashMap.put(sku.getSkuSpec(),sku.getId());
        }

        ProductItemVo vo = new ProductItemVo();
        vo.setProduct(product);
        vo.setProductSku(productSku);
        vo.setSliderUrlList(sliderUrl);
        vo.setDetailsImageUrlList(detailsImageUrl);
        vo.setSpecValueList(array);
        vo.setSkuSpecValueMap(stringObjectHashMap);


        return Result.build(vo,ResultCodeEnum.SUCCESS);
    }

    public List<Category> categoryList(){
        String oneLevel = redisTemplate.boundValueOps(RedisCommot.ONE_LEVEL).get();
        List<Category> categoryList=null;
        if (oneLevel==null){
            List<Category> categories = categoryMapper.findC();
            redisTemplate.boundValueOps(RedisCommot.ONE_LEVEL).set(JSON.toJSONString(categories));
            return categories;
        }
        categoryList=JSON.parseArray(oneLevel, Category.class);
        return categoryList;
    }
}
